package LeetCode;

import java.util.Stack;

public class LC_591_TagValidator{

    public static void main(String[] args) {

    }

    public class Solution {
        Stack< String > stack = new Stack < > ();
        boolean contains_tag = false;
        boolean isValidTagName(String s, boolean ending) {
            if (s.length() < 1 || s.length() > 9)
                return false;
            for (int i = 0; i < s.length(); i++) {
                if (!Character.isUpperCase(s.charAt(i)))
                    return false;
            }
            if (ending) {
                if (!stack.isEmpty() && stack.peek().equals(s))
                    stack.pop();
                else
                    return false;
            } else {
                contains_tag = true;
                stack.push(s);
            }
            return true;
        }
        boolean isValidCdata(String s) {
            return s.indexOf("[CDATA[") == 0;
        }
        public boolean isValid(String code) {
            if (code.charAt(0) != '<' || code.charAt(code.length() - 1) != '>')
                return false;
            for (int i = 0; i < code.length(); i++) {
                boolean ending = false;
                int closeindex;
                if(stack.isEmpty() && contains_tag)
                    return false;
                if (code.charAt(i) == '<') {
                    if (!stack.isEmpty() && code.charAt(i + 1) == '!') {
                        closeindex = code.indexOf("]]>", i + 1);
                        if (closeindex < 0 || !isValidCdata(code.substring(i + 2, closeindex)))
                            return false;
                    } else {
                        if (code.charAt(i + 1) == '/') {
                            i++;
                            ending = true;
                        }
                        closeindex = code.indexOf('>', i + 1);
                        if (closeindex < 0 || !isValidTagName(code.substring(i + 1, closeindex), ending))
                            return false;
                    }
                    i = closeindex;
                }
            }
            return stack.isEmpty() && contains_tag;
        }
    }

}